﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace UI
{
    public class VipBenefitWeeklyReward : MonoBehaviour
    {
        public UILabel labDiscount;
        public UILabel labTitle;
        public UILabel labLeftBuyTimes;
        public GameObject discountRoot;
        public GameObject btnBuy;
        public GameObject buyed;
        public UISprite costLogo;
        public UILabel labPrice;
        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;

        UIActivityVipBenefits vipBenefits;

        ActivityShopItem rewardInfo;

        public void SetReward(ActivityShopItem item, UIActivityVipBenefits v)
        {
            vipBenefits = v;
            rewardInfo = item;

            float disc = CommonMethod.ParseDiscount(item.discount);
            if (disc > 0 && disc < 10)
            {
                discountRoot.SetActive(true);
                labDiscount.text = disc.ToString();
            }
            else
            {
                discountRoot.SetActive(false);
            }

            labTitle.text = item.name;

            labLeftBuyTimes.text = item.left.ToString() + "/" + item.total.ToString();

            //领取状态
            btnBuy.SetActive(false);
            buyed.SetActive(false);
            costLogo.gameObject.SetActive(false);
            labPrice.gameObject.SetActive(false);
            if(item.status == 2)
            {
                buyed.SetActive(true);
                buyed.GetComponent<UISprite>().spriteName = "UI_activity_meiriqiandao_yilingqu";
            }
            else //if(item.status == 1)
            {
                costLogo.gameObject.SetActive(true);
                labPrice.gameObject.SetActive(true);
                labPrice.text = item.costResource[0].num.ToString();
                btnBuy.SetActive(true);
            }
            //else if(item.status == 0)
            //{
            //    buyed.SetActive(true);
            //    buyed.GetComponent<UISprite>().spriteName = "UI_activity_weiwancheng";
            //}

            //奖励物品
            foreach (var c in simpleGrid.childs)
            {
                Destroy(c);
            }
            simpleGrid.childs.Clear();

            GameObject commonicon = AssetLoad.ResourceManager.Instance.LoadResource("prefab/CommonIcon") as GameObject;
            foreach (var i in item.getResource)
            {
                GameObject obj = Instantiate(commonicon);
                obj.GetComponent<CommonIcon>().SetData(i.id, true, i.num);
                simpleGrid.AddChild(obj);
            }
            scrollView.ResetPosition();
        }

        public void OnBuyBtn()
        {
            if(StaticData.playerData.diamond < rewardInfo.costResource[0].num)
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("diamond_lack"));
                return;
            }

            if(rewardInfo.pickOneOrGetAll == 1)
            {
                //全部
                CSGetVipWeekBoonReward msg = new CSGetVipWeekBoonReward();
                foreach (var i in rewardInfo.costResource)
                    msg.costResources.Add(i);
                foreach (var i in rewardInfo.getResource)
                    msg.getResources.Add(i);
                msg.index = rewardInfo.index;

                NetAPI.SendHttp(OpDefine.CSGetVipWeekBoonReward, msg, OnGetRewardRecv);
            }
            else
            {
                //x选1
                PageManager.Instance.OpenPage("SelectAcRewardPage", "");
                SelectAcRewardPage reward = PageManager.Instance.GetDictPage("SelectAcRewardPage") as SelectAcRewardPage;
                reward.m_click = this.SelectAcAward;
                reward.SetReward(rewardInfo.getResource);
            }
        }
        void SelectAcAward(BaseResource res)
        {
            CSGetVipWeekBoonReward msg = new CSGetVipWeekBoonReward();
            msg.getResources.Add(res);
            foreach (var i in rewardInfo.costResource)
                msg.costResources.Add(i);
            msg.index = rewardInfo.index;

            NetAPI.SendHttp(OpDefine.CSGetVipWeekBoonReward, msg, OnGetRewardRecv);
        }

        void OnGetRewardRecv(NetWork.Packets.Packet packet, bool success)
        {
            if(success)
            {
                SCGetVipWeekBoonReward msg = packet.kBody as SCGetVipWeekBoonReward;
                StaticData.playerData.AddResultObj(msg.get);
                StaticData.playerData.Delete(msg.costResources);

                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("buy_success"));
                //CommonMethod.ShowGetObjs(msg.get);

                vipBenefits.UpdateWeeklyTab(msg.week);
            }
        }
    }
}

